'''
Author: LIMBO.jyc
Date: 2022-03-09 17:55:08
Last Modified by: LIMBO.jyc
Description: 
'''
from re import match
from py2neo import Graph, Node, Relationship
from py2neo.matching import *
import pprint

# Graph()中第一个为local host链接，auth为认证，包含 username 和 password
graph = Graph('http://localhost:7474', auth = ('neo4j', '123456'))

graph.delete_all()
#filename  = "./test.txt"
filename  = "./bash_sym_dep.rsf"
filename1 = "./bash_gt.rsf"

matcher = NodeMatcher(graph)

file_to_module={}
overlap_file=[]
moduules=[]

with open (filename1,'r') as file:
    while True:
        lines = file.readline()
        lines = lines[:-1]
        this_line  = lines.split(" ")
        if not lines:
            break
        # 如果不包含文件
        if this_line[2] not in file_to_module:
            file_to_module[this_line[2]]=this_line[1]
        else:
            overlap_file.append(this_line[2])
        if this_line[1] not in moduules:
            moduules.append(this_line[1])

for overlap in overlap_file:
    print(overlap)

pprint.pprint(file_to_module)


with open (filename,'r') as file:
    while True:
        lines = file.readline()
        lines = lines[:-1]
        this_line  = lines.split(" ")
        if not lines:
            break
        #print(this_line)
        

        label1 = -1
        if this_line[1] in file_to_module:
            label1 = moduules.index(file_to_module[this_line[1]])+3
        elif this_line[1] in overlap_file:
            label1 = 1
        else:
            label1 = 2  

        label2 = -1
        if this_line[2] in file_to_module:
            label2 = moduules.index(file_to_module[this_line[2]])+3
        elif this_line[2] in overlap_file:
            label2 = 1
        else:
            label2 = 2
        


        result1 = matcher.match('file'+str(label1),name = this_line[1]).first()
        result2 = matcher.match('file'+str(label2),name = this_line[2]).first()
        #print(result1)


        if result1 == None:
            node1 = Node('file'+str(label1), name = this_line[1])
            graph.create(node1)
            #print(node1)           
        if result2 == None:
            node2 = Node('file'+str(label2), name = this_line[2])
            graph.create(node2)
            #print(node2)

        node1 = matcher.match('file'+str(label1),name = this_line[1]).first()
        node2 = matcher.match('file'+str(label2),name = this_line[2]).first()
        print(node1)
        print(node2)
        ab = Relationship(node1, ' ', node2)
        graph.create(ab)

